/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package beans;

import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author machine
 */
@Stateless
public class PeliculasFacade extends AbstractFacade<Peliculas> implements PeliculasFacadeLocal {
    @PersistenceContext(unitName = "videosJEE-ejbPU")
    private EntityManager em;

    @Override
    protected EntityManager getEntityManager() {
        return em;
    }

    public PeliculasFacade() {
        super(Peliculas.class);
    }
    
    @Override
    public List<Peliculas> consultarPeliculasPorCategoria(String categoria, String idvideo) {
        
        //String queryString = "SELECT p FROM Peliculas p JOIN o.Categorias c WHERE c.idcategorias = :idcateg";
        //Query query = em.createNamedQuery("Peliculas.findAll"); //createQuery(queryString);
        //query.setParameter("idcateg", Integer.getInteger(categoria) );
        /*
        Query query = em.createQuery( "SELECT * FROM Peliculas WHERE Peliculas.Categorias.idcategorias = ?1" );
        query.setParameter( 1, Integer.getInteger(categoria) );
        */
        
        String CondVideo = "";
        if( idvideo != null ){
            CondVideo += " AND p.idpeliculas = "+idvideo+" ";
        }
        
        
        //String queryString = ;
        Query q = em.createQuery( "SELECT p FROM Peliculas p WHERE p.idcategorias.idcategorias = " + categoria + CondVideo + " ORDER BY p.idpeliculas " );
        //q.setParameter("idcateg", Integer.getInteger("1"));
        
        return q.getResultList(); // or getSingleResult();
        
    }

    @Override
    public List<Peliculas> resultadoConsulta(String[] condiciones) {
        
        String cond = "";
        String froms = "";
        String joins = "";
        String group = "";
        
        
        for (int i = 0; i < condiciones.length; i++) {
            
            if( condiciones[i] != null && condiciones[i].length() > 0  ){
                switch ( i ){
                    case 0 : cond += " AND pel.idcategorias.idcategorias = "+Integer.parseInt(condiciones[i])+" "; break;
                    case 1 : cond += " AND pel.idcategorias.idcategorias != "+Integer.parseInt(condiciones[i]); break;
                    case 2 : cond += " AND pel.ano >= "+condiciones[i]; break;
                    case 3 : cond += " AND pel.ano <= "+condiciones[i]; break;
                    case 4 : cond += " AND pel.duracion "+condiciones[i+1]+" "+Integer.parseInt(condiciones[i]); break;
                    case 6 : froms += " , Calificacion cal ";
                             cond += " AND cal.idpeliculas.idpeliculas = pel.idpeliculas AND cal.calificacion = "+Short.parseShort(condiciones[i])+" "; 
                             if( condiciones[i+1] != null){
                                 group += " group by pel.idpeliculas " +
                                            " having ( count(pel.idpeliculas) ) "+condiciones[i+2]+" "+Integer.parseInt(condiciones[i+1]);
                                          
                             }
                             break;
                }
            }
            
        }
        
        String hql = "select distinct pel from Peliculas pel "+froms+" "+joins+" where pel.idcategorias.idcategorias < 1000 "+cond+group;
        //String hql = "select pel from Peliculas pel";
        
        Query q = em.createQuery( hql );
        
        return q.getResultList();
        
    }

    @Override
    public Peliculas consultarPelicula(String peli) {
        Query q = em.createQuery( "SELECT p FROM Peliculas p WHERE p.idpeliculas = " + peli + " " );
        
        return (Peliculas) q.getSingleResult();
    }
    
}
